package com.webchat.chat.core.task;

import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.webchat.chat.core.redis.RedisService;
import com.webchat.chat.core.tools.RedisConstant;
import com.webchat.chat.core.tools.SocketUtil;
import com.webchat.chat.core.tools.TaskConstant;
import com.webchat.chat.model.SocketClient;
import com.webchat.chat.service.ClientServiceRecordService;
import com.webchat.chat.service.RoomClientService;
import com.webchat.chat.service.SocketClientService;
import com.webchat.chat.service.SocketRoomService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * Created by Owner on 2018/6/30.
 */
@Component
public class MessageTask {

    private static final Logger log = LoggerFactory.getLogger(MessageTask.class);

    @Resource
    private RedisService redisService;
    @Resource
    private SocketIOServer socketIOServer;
    @Resource
    private SocketRoomService socketRoomService;
    @Resource
    private SocketClientService socketClientService;
    @Resource
    private RoomClientService roomClientService;
    @Resource
    private ClientServiceRecordService clientServiceRecordService;

    //每分钟执行
    /**
     * 查找可用客服
     */
    @Scheduled(cron = TaskConstant.SEARCH_SC_TIME_INTERVAL)
    public void searchSC() {
        Set<String> allKeys = redisService.getAllKeys();
        ExecutorService executor = Executors.newCachedThreadPool();
        allKeys.forEach(k -> {
            if (k.startsWith(RedisConstant.SCOKET_CLIENT_WAIT_C)) {
                Map<String, SocketClient> waitCustomerClients = redisService.getMap(k);

                Future<Void> dealWaitCustomerCallable = executor.submit(new Callable<Void>() {
                    @Override
                    public Void call() throws Exception {
                        waitCustomerClients.forEach((key, value) -> {
                            if (value != null) {
                                SocketIOClient socketIOClient = SocketUtil.getSocketIOClient(value, socketIOServer);
                                if(socketIOClient != null){
                                    log.info("[{}]存在等待的客户,现在开始处理", k);
                                    long s = System.currentTimeMillis();
                                    //未分配成功的客户集合
                                    SocketUtil.allotCS(socketIOClient, value.getUserId(),value.getTenantCode(),
                                            redisService, socketIOServer, roomClientService, socketRoomService,
                                            socketClientService, clientServiceRecordService);
                                    long e = System.currentTimeMillis();
                                    log.info("[{}]等待的客户处理完成,耗时{}ms", k, (e - s));
                                }
                            }
                        });
                        return null;
                    }
                });
            }
        });
        executor.shutdown();
    }
}
